package com.shadow.mall.market.controller;

import com.shadow.mall.common.core.JsonResult;
import com.shadow.mall.common.exception.BaseBizException;
import com.shadow.mall.market.api.MarketApi;
import com.shadow.mall.market.domain.dto.CalculateOrderAmountDTO;
import com.shadow.mall.market.domain.dto.UserCouponDTO;
import com.shadow.mall.market.domain.query.UserCouponQuery;
import com.shadow.mall.market.domain.request.CalculateOrderAmountRequest;
import com.shadow.mall.market.domain.request.LockUserCouponRequest;
import com.shadow.mall.market.service.CouponService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/market")
public class MarketController {

    @RequestMapping("/test")
    public void test(){
        marketApi.test();
    }

    @Autowired
    MarketApi marketApi;

    CouponService couponService;

    //远程调用
    @RequestMapping("/calculateOrderAmountMarket")
    public JsonResult<CalculateOrderAmountDTO> calculateOrderAmount(@RequestBody CalculateOrderAmountRequest calculateOrderPriceRequest){
        return marketApi.calculateOrderAmount(calculateOrderPriceRequest);
    }

    //远程调用
    @RequestMapping("/lockUserCouponMarket")
    public JsonResult<Boolean> lockUserCoupon(@RequestBody LockUserCouponRequest lockUserCouponRequest, HttpServletResponse response){
        JsonResult<Boolean> booleanJsonResult =  null;
        booleanJsonResult = marketApi.lockUserCoupon(lockUserCouponRequest);
        if(StringUtils.isNoneEmpty(booleanJsonResult.getErrorCode())){
             throw new BaseBizException(booleanJsonResult.getErrorCode(),booleanJsonResult.getErrorMessage());
        }
        return booleanJsonResult;
    }


    @RequestMapping("/getUserCouponMarket")
    public JsonResult<UserCouponDTO> getUserCoupon(@RequestBody UserCouponQuery userCouponQuery){
        JsonResult<UserCouponDTO> booleanJsonResult = marketApi.getUserCoupon(userCouponQuery);
        return booleanJsonResult;
    }



}
